﻿  
  --get the addon namespace
  local addon, ns = ...
  
  --get the config values
  local LabelUI = ns.LabelUI
  local cfg = ns.cfg

  local noshow = LabelUI.noshow
  local Kill = LabelUI.Kill
  local point = LabelUI.point
  
  local pos = { a1 = "BOTTOMRIGHT", a2 = "BOTTOMRIGHT", af = "UIParent", x = -3, y = 3 }
  
  local f = CreateFrame("Frame", nil, UIParent)
  
  local ToolTipPoint = function(frame)
  	frame:SetPoint(pos.a1,pos.af,pos.a2, pos.x, pos.y) -- -82
  end

  local DropDownPoint = function(frame)  
	 frame.xOffset = pos.x
	 frame.yOffset = pos.y
	 frame.relativeTo = pos.af
	 frame.point = pos.a1
	 frame.relativePoint = pos.a2
  end
  
  local UpdatePos = function(frame, x, y, a1, a2)
	 pos.af = frame
	 pos.x = x
	 pos.y = y
	 pos.a1 = a1
	 pos.a2 = a2
  end
  
  local ScanPoints = function(self, base)
     local tempos
	 local maxcur = 0
	 for id, var in pairs (base) do
	 	if id > maxcur and var.enable then
			tempos = var
			maxcur = id
		end
	 end
	 if maxcur == 0 then return end
	 if tempos.a1 then
	 	UpdatePos(tempos.frame, tempos.x, tempos.y, tempos.a1, tempos.a2)
	 else
	 	UpdatePos(tempos.frame, tempos.x, tempos.y, "BOTTOMRIGHT", "TOPRIGHT")
	 end
  end
  
  local GameTooltipDefTooltip = function()
  		local tooltip = _G.GameTooltip
		if tooltip then
 			tooltip:ClearAllPoints()
			ToolTipPoint(tooltip)
		end
  end
  
  local GameTooltipToast = function()
  		local toastframe = _G.BNToastFrame
		if toastframe then
			toastframe:ClearAllPoints()
			ToolTipPoint(toastframe)
		end
  end
 
  local function Init()

	LabelUI.TTPoints.Update = ScanPoints
	LabelUI.TTPoints:Set()
	LabelUI.ToolTipPoint = ToolTipPoint
	LabelUI.DropDownPoint = DropDownPoint
	
	hooksecurefunc("GameTooltip_SetDefaultAnchor", GameTooltipDefTooltip)
	_G.BNToastFrame:HookScript("OnShow", GameTooltipToast)
	GameTooltipToast()
  	
  end
  LabelUI:AddInit(Init, "ToolTips")